package ar.com.mooral.activities;

import java.util.Collections;
import java.util.Hashtable;
import java.util.Vector;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.crohn.view.CrohnButton;
import ar.com.mooral.crohn.view.CrohnImageView;
import ar.com.mooral.data.Medications;
import ar.com.mooral.data.PatientSymptoms;
import ar.com.mooral.data.Symptoms;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.PatientSymptomPojo;
import ar.com.mooral.pojo.SymptomPojo;
import ar.com.mooral.pojo.util.PojoComparatorDate;
import ar.com.mooral.pojo.util.PojoComparatorName;


public class SintomasPersonalizados extends CrohnActivity {


	private Context context;
	private Vector<CrohnImageView> deleteImageViews;
	private Vector<CrohnImageView> editImageViews;
    
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sintomas_personalizados);

        
        
		// EDIT
        Button btnSintomasPersonalizadosEditar = (Button) findViewById(R.id.btnSintomasPersonalizadosEditar);
        btnSintomasPersonalizadosEditar.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	for (ImageView img : editImageViews ) {
		    		img.setVisibility((img.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE));
		        }
		    }
		});
        
        
		// DELETE
        Button btnSintomasPersonalizadosBorrar = (Button) findViewById(R.id.btnSintomasPersonalizadosBorrar);
        btnSintomasPersonalizadosBorrar.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	for (ImageView img : deleteImageViews ) {
		    		img.setVisibility((img.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE));
		        }
		    }
		});		
        
        
        
		
		// ADD
        Button btnSintomasPersonalizadosAgregar = (Button) findViewById(R.id.btnSintomasPersonalizadosAgregar);
        btnSintomasPersonalizadosAgregar.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				CrohnContext.setActivityPatientSymptomLoaded(false);
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(	SintomaPersonalizadoNuevo.class.getPackage().getName(), 
														SintomaPersonalizadoNuevo.class.getName()));
				startActivity(intent);
		    }
		});		

		// CANCELAR
        Button btnSintomasPersonalizadosRetorno = (Button) findViewById(R.id.btnSintomasPersonalizadosRetorno);
        btnSintomasPersonalizadosRetorno.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    	
		        //TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	            //parentActivity.onBackPressed();

		    }
		});		

    }

    
	protected void onStart() {

		
		super.onStart();

		
		
		if(!CrohnContext.isActivityPatientSymptomLoaded()){
			CrohnContext.setActivityPatientSymptomLoaded(true);
		}else{
			CrohnContext.setActivityPatientSymptomLoaded(false);
//			finish();
	        //TabGroupActivity parentActivity = (TabGroupActivity)getParent();
            //parentActivity.onBackPressed();

		}
		
		

        // AGREGO LOS MEDICAMENTOS
        LinearLayout linearLayoutSintomasPersonalizados = (LinearLayout)findViewById(R.id.linearLayoutSintomasPersonalizados);
        linearLayoutSintomasPersonalizados.removeAllViews();
        
        Symptoms symptoms = new Symptoms(this);
        Vector<Hashtable<String, String>> datas = (Vector<Hashtable<String, String>>)symptoms.findAll();
        
        Collections.sort(((Vector)datas), new PojoComparatorName());
        deleteImageViews = new Vector<CrohnImageView>();
        editImageViews = new Vector<CrohnImageView>();

        for(Hashtable<String,String> data: datas){
        	BasePojo pojo = (BasePojo)data;
        	
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 45));
			ll.setOrientation(LinearLayout.HORIZONTAL);
			ll.setBackgroundResource(R.drawable.medication_bg);
			ll.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
			linearLayoutSintomasPersonalizados.addView(ll);			

			
			
			if (Integer.parseInt(pojo.getId()) > 6) {
			
				CrohnImageView deleteImageView = new CrohnImageView(this);
				deleteImageView.setBackgroundResource(R.drawable.stop);
				deleteImageView.setLayoutParams(new LayoutParams(20, 20));
				deleteImageView.setVisibility(View.GONE);
				deleteImageView.setPojo(pojo);
				deleteImageView.setOnClickListener(new View.OnClickListener() {
	    		    public void onClick(View view) {
	    		    	BasePojo basePojo = ((CrohnImageView) view).getPojo();
	    		    	basePojo.getDatabaseHelper().delete(basePojo.getId());
	    		    	((LinearLayout) view.getParent().getParent()).removeView((View) view.getParent());
	    		    }
	            });
				ll.addView(deleteImageView);
				
				
				CrohnImageView editImageView = new CrohnImageView(this);
				editImageView.setBackgroundResource(R.drawable.edit);
				editImageView.setLayoutParams(new LayoutParams(20, 20));
				editImageView.setVisibility(View.GONE);
				editImageView.setPojo(pojo);
				editImageView.setOnClickListener(new View.OnClickListener() {
	    		    public void onClick(View view) {
	
	    				Intent intent = new Intent();
	    				CrohnContext.setPojo(((CrohnImageView)view).getPojo());
	    				intent.setComponent(new ComponentName(	SintomaPersonalizadoNuevo.class.getPackage().getName(), 
	    														SintomaPersonalizadoNuevo.class.getName()));
	    				//startActivity(intent);
	             		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	             		parentActivity.startChildActivity("SintomaPersonalizadoNuevo", intent);
	
	    		    }
	            });
				ll.addView(editImageView);
			
			
				deleteImageViews.add(deleteImageView);
				editImageViews.add(editImageView);
        	}
			
			
			
			//Button recordButton = new Button(this);
			CrohnButton recordButton = new CrohnButton(this);
			//recordButton.setText(record.get("nombre"));
			
			String text = pojo.getName();
			if(text.length()>30) text = text.substring(0,30) + "...";
			recordButton.setText(text);
			recordButton.setLayoutParams(new LayoutParams(215, 30));
			recordButton.setBackgroundResource(R.drawable.white);
			recordButton.setTextSize(10);
			recordButton.setPojo(pojo);
			recordButton.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	BasePojo basePojo = ((CrohnButton) view).getPojo();
    		    	
    		    	PatientSymptomPojo patientSymptomPojo = new PatientSymptomPojo();
    		    	
    		    	patientSymptomPojo.setName(basePojo.getName());
    		    	patientSymptomPojo.setDate(CrohnContext.getDateTimeNow());
    		    	
		    		CrohnContext.setPojo(patientSymptomPojo);
    		    	
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(	SintomaPersonalizado.class.getPackage().getName(), 
							SintomaPersonalizado.class.getName()));
					
	         		//TabGroupActivity parentActivity = (TabGroupActivity)((TabGroupActivity)getParent()).getParent();
					TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	         		parentActivity.startChildActivity("SintomaPersonalizado", intent);

					//startActivity(intent);
    		    }
            });
			ll.addView(recordButton);
			
        }

    	
    }

}
